package com.example.module_patrol.ui.notdoing

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.utils.ToastUtils
import com.example.module_patrol.R
import com.example.module_patrol.adapter.DateAdapter
import com.example.module_patrol.adapter.PatrolAdapter
import com.example.module_patrol.adapter.PatrolNotDoingAdapter
import com.example.module_patrol.databinding.ActivityNotDoingBinding
import com.example.module_patrol.ui.PatrolViewmodel
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@Route(path = "/patrol/NotDoingActivity")
@AndroidEntryPoint
class NotDoingActivity : BaseActivity<PatrolViewmodel,ActivityNotDoingBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_not_doing
    private val mViewModel: PatrolViewmodel by viewModels()
    lateinit var pkAdapter: PatrolNotDoingAdapter
    private var back: ImageView? = null
    private var more: ImageView? = null

    private var currentWeekOffset = 0 // 0=本周，1=下周，-1=上周
    private lateinit var adapter: DateAdapter
    override fun initView() {
        pkAdapter = PatrolNotDoingAdapter()
        mBinding.rvTask.adapter = pkAdapter
        mBinding.rvTask.layoutManager = LinearLayoutManager(this)
        mViewModel.getPatrolTaskByStatus("0")
//        mViewModel.getPatrolTaskByDate("2025-03-08","2025-03-08")
        /*
               返回键
                */
        back=mBinding.topBar4.findViewById(R.id.back)
        back!!.setOnClickListener {
            finish()
        }
        more=mBinding.topBar4.findViewById(R.id.more)
        more!!.setOnClickListener {
           ToastUtils.showShort(this,"功能开发中...")
        }

        pkAdapter = PatrolNotDoingAdapter()
        mBinding.rvTask.adapter = pkAdapter
        mBinding.rvTask.layoutManager = LinearLayoutManager(this)
        mViewModel.getPatrolTaskByStatus("0")

        // 初始化日期选择器
        setupDateSelector()  // <-- 添加这一行

        back = mBinding.topBar4.findViewById(R.id.back)
        back!!.setOnClickListener { finish() }
        more = mBinding.topBar4.findViewById(R.id.more)
        more!!.setOnClickListener { ToastUtils.showShort(this, "功能开发中...") }

    }

    override fun initData() {

        mViewModel.patrolTaskListLiveData.observe(this){
            if (it.isEmpty()){
                ToastUtils.showShort(this,"暂无数据")
            }
            pkAdapter.addParkingList(it)
        }
//        mViewModel.patrolTaskListDateLiveData.observe(this){
//            if (it.isEmpty()){
//                ToastUtils.showShort(this,"暂无数据")
//            }
//            pkAdapter.addParkingList(it)
//        }
    }
    // 获取当前周的日期范围（周一到周日）
    fun getCurrentWeekDates(): List<Pair<String, String>> { // Pair<星期, 日期>
        val calendar = Calendar.getInstance()
        val currentDay = calendar.get(Calendar.DAY_OF_MONTH)

        // 调整到本周一（可根据需求改为周日开始）
        calendar.firstDayOfWeek = Calendar.MONDAY
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)

        val weekDates = mutableListOf<Pair<String, String>>()
        val weekDays = listOf("一", "二", "三", "四", "五", "六", "日")

        repeat(7) { index ->
            val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
            weekDates.add(weekDays[index] to "$dayOfMonth")
            calendar.add(Calendar.DATE, 1) // 下一天
        }

        return weekDates
    }
    private fun setupDateSelector() {
        val recyclerView = findViewById<RecyclerView>(R.id.rvDates)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)

        refreshWeekDates()

        // 上一周按钮
        findViewById<View>(R.id.btnPrevWeek).setOnClickListener {
            currentWeekOffset--
            refreshWeekDates()
        }

        // 下一周按钮
        findViewById<View>(R.id.btnNextWeek).setOnClickListener {
            currentWeekOffset++
            refreshWeekDates()
        }
    }

    private fun refreshWeekDates() {
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.WEEK_OF_YEAR, currentWeekOffset) // 调整周偏移

        val currentDay = if (currentWeekOffset == 0) {
            calendar.get(Calendar.DAY_OF_MONTH)
        } else {
            -1 // 非本周不高亮任何日期
        }

        val weekDates = getWeekDates(calendar)
        adapter = DateAdapter(weekDates, currentDay)
        findViewById<RecyclerView>(R.id.rvDates).adapter = adapter
    }

    // 获取任意一周的日期
    private fun getWeekDates(calendar: Calendar): List<Pair<String, String>> {
        val tempCalendar = calendar.clone() as Calendar
        tempCalendar.firstDayOfWeek = Calendar.MONDAY
        tempCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)

        return (0 until 7).map { index ->
            val weekDay = when (index) {
                0 -> "一"
                1 -> "二"
                // ... 其他星期
                else -> "日"
            }
            val day = tempCalendar.get(Calendar.DAY_OF_MONTH).toString()
            tempCalendar.add(Calendar.DATE, 1)
            weekDay to day
        }
    }
    private fun scrollToToday() {
        val todayIndex = adapter.getTodayPosition()
        if (todayIndex != -1) {
            mBinding.rvDates.scrollToPosition(todayIndex)
        }
    }


//    // 添加以下代码使滑动更流畅
//    SnapHelper().attachToRecyclerView(recyclerView)

}